<?php

use Biz\model\GoodsTestActivity\FineGoodsContentModel;
use Biz\model\GoodsTestActivity\FineGoodsJoinModel;
use Biz\model\GoodsTestActivity\FineGoodsJoinRecordModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\service\GoodsTestActivity\FineGoodsService;

/**
 * 我参与的
 */
class MyApplyAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;

    private $page = 1;
    private $field = [];
    private $joinActivityIds = [];//当前用户申请过的所有活动id
    private $joinSelectedActivityIds = [];//当前用户在获选名单中的活动id

    public function main()
    {
        $type = $this->getRequest()->getQuery('type', 0);//0：全部 1：申请中 2：体验中 3：评测报告
        $page = $this->getRequest()->getQuery('page', 1);
        $this->page = $page > 1 ? $page : 1;
        //获取活动列表
        $this->field = [
            'id',
            'type',
            'brand_id',
            'brand_name',
            'basic_title',
            'activity_title',
            'goods_num',
            'goods_worth',
            'goods_max_worth',
            'schedule_start_time',
            'schedule_end_time',
            'activity_state',
            'basic_id',
            'cover_img',
            'report_num',
            'evaluation_id'
        ];
        $this->getJoinRecord();
        $data = [
            'list' => [],
            'is_over' => 1,
        ];
        if ($this->joinActivityIds) {
            switch ($type) {
                case 0://全部
                    $actIngList = $this->getActIngList();
                    $actLearnList = $this->getActLearnList();
                    $list = array_merge($actIngList, $actLearnList);
                    break;
                case 1://申请中
                    $list = $this->getActIngList();
                    break;
                case 2://体验中
                    $list = $this->getActLearnList();
                    break;
                case 3://评测报告
                    $list = $this->getReportList();
                    break;
            }
            $data = [
                'list' => $list,
                'is_over' => 1
            ];
        }
        $this->setSuccess($data);
    }

    private function getJoinRecord()
    {
        $this->joinActivityIds = FineGoodsJoinRecordModel::all(['uid' => $this->uid], 'fine_goods_id');
        $this->joinSelectedActivityIds = $this->joinActivityIds ?
            FineGoodsJoinModel::all(['uid' => $this->uid, 'join_status' => FineGoodsJoinModel::JOIN_STATUS_PREFER, 'fine_goods_id' => $this->joinActivityIds], 'fine_goods_id')
            : [];
    }

    /**
     * 获取我正在申请中的活动
     * @param $field
     * @return array
     */
    private function getActIngList()
    {
        $where = [
            'id' => $this->joinActivityIds,
            'activity_state' => FineGoodsModel::STATE_PASS,
            'schedule_start_time[<=]' => time(),
            'schedule_end_time[>]' => time(),
            'ORDER' => ['schedule_start_time' => 'DESC', 'id' => 'DESC']
        ];
        $list = FineGoodsModel::all($where, $this->field);
        $list = FineGoodsService::getFineGoodsPv($list, false);
        return $list;
    }

    /*
     * 获取我正在体验中的活动
     */
    private function getActLearnList()
    {
        $where = [
            'id' => $this->joinSelectedActivityIds,
            'activity_state' => FineGoodsModel::STATE_PASS,
            'ORDER' => ['schedule_end_time' => 'DESC'],
        ];
        $list = FineGoodsModel::all($where, $this->field);
        $contentList = $this->joinSelectedActivityIds ? FineGoodsContentModel::all([ // 查询当前用户在指定活动中是否发布了测评报告
            'uid' => $this->uid,
            'fine_goods_id' => $this->joinSelectedActivityIds,
            'is_delete' => FineGoodsContentModel::IS_DELETE_FALSE,
        ], ['obj_id', 'fine_goods_id']) : [];
        $res = [];
        if ($list) {
            $now = time();
            $contentMap = [];
            if ($contentList) {
                $contentMap = array_column($contentList, 'obj_id', 'fine_goods_id');
            }
            foreach ($list as $item) {
                //只有活动结束，名单被审核公布，且用户在当前活动没有发布报告时 才展示该活动
                if ($now > ($item['schedule_end_time'] + FineGoodsModel::VERIFY_USER_TIME) && !isset($contentMap[$item['id']])) {
                    $item['activity_state_name'] = '体验中';
                    $res [] = $item;
                }
            }
            $res = FineGoodsService::getFineGoodsPv($res, false);
        }

        return $res;
    }

    /**
     * 获取我被选中的活动，已经产生的评测报告
     * @return array
     */
    private function getReportList()
    {
        $where = [
            'id' => $this->joinSelectedActivityIds,
            'activity_state' => FineGoodsModel::STATE_PASS,
            'schedule_end_time[<]' => time(),
            'schedule_end_time[!]' => 0,
            'report_num[>]' => 0,
            'ORDER' => ['schedule_end_time' => "DESC"],
        ];
        $allList = FineGoodsModel::all($where, $this->field);
        $list = [];
        if ($allList) {
            $ids = [];
            foreach ($allList as $item) {
                //只筛选 体验中  报告已产出的活动数据
                if ($item['activity_state'] > FineGoodsModel::STATE_VERIFY) {
                    $ids [] = $item['id'];
                }
            }
            $contents = $this->getNoteListById($ids);
            foreach ($allList as $v) {
                if (isset($contents[$v['id']]) && $contents[$v['id']]) {
                    $v['report_list'] = $contents[$v['id']];
                    $list[] = $v;
                }
            }
        }
        return $list;
    }

    private function getNoteListById($ids)
    {
        $list = [];
        //获取全部内容obj_id
        $where = [
            'uid' => $this->uid,
            'fine_goods_id' => $ids,
            'ORDER' => ["points" => "DESC", 'addtime' => "DESC"],
            'is_delete[!]' => FineGoodsContentModel::IS_DELETE_TRUE,
        ];
        $reports = $ids ? FineGoodsContentModel::all($where, ['obj_id', 'fine_goods_id']) : [];
        if (!$reports) {
            return $list;
        }
        $obj_ids = array_column($reports, 'obj_id');
        $report_list = FineGoodsService::getNoteListByObjIds($obj_ids, $this->uid);
        if (!$report_list) {
            return $list;
        }
        //获取内容
        foreach ($reports as $report) {
            if (isset($report_list[$report['obj_id']]) && $report_list[$report['obj_id']]) {
                $list[$report['fine_goods_id']][] = $report_list[$report['obj_id']];
            }
        }
        return $list;
    }

}


